package cn.imvc.app.controllers;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.imvc.app.entities.Article;
import cn.imvc.app.entities.ArticleClass;
import cn.imvc.app.models.ArticleClassModel;
import cn.imvc.app.models.ArticleModel;
import cn.imvc.app.models.BrandModel;
import cn.imvc.app.models.MemberModel;

@Controller
@RequestMapping("article")
public class ArticleControllerImpl implements IndexController {

	@Autowired
	MemberModel memberModel;

	@Autowired
	ArticleModel articleModel;

	@Autowired
	BrandModel brandModel;

	@Autowired
	ArticleClassModel articleClassModel;

	@RequestMapping("/article.html")
	public String article(Model model, Integer articleClassId) {
		model.addAttribute("articleClassId", articleClassId);
		ArticleClass articleClass = articleClassModel.get(articleClassId);
		model.addAttribute("articleClassTitle", articleClass.getName());
		return "article/article";
	}

	@RequestMapping("/home.html")
	public String home(Model model) {
		model.addAttribute("article1", articleModel.get(33));
		model.addAttribute("article2", articleModel.get(34));
		model.addAttribute("article3", articleModel.get(35));
		model.addAttribute("article4", articleModel.get(36));
		model.addAttribute("brandList", brandModel.getAllBrand());
		
		return "article/home";
	}

	@RequestMapping("/list/{articleClassId}/{page}.html")
	public String list(Model model, @PathVariable Integer articleClassId, @PathVariable Integer page) {
		Page<Article> articleList = articleModel.findArticleListByArticleClass(articleClassId, page, null);
		if(articleList == null){
			model.addAttribute("articleList", null);
			model.addAttribute("over", true);
		} else {
			model.addAttribute("articleList", articleList.getContent());
			model.addAttribute("over", false);
		}
		return "article/list";
	}

	@RequestMapping("/detail/{id}.html")
	public String detail(Model model, @PathVariable String id) {
		Pattern pattern = Pattern.compile("[0-9]+");
		Matcher matcher = pattern.matcher(id);
		boolean result = matcher.matches();
		Article article = null;
		if (result) {
			article = articleModel.get(Integer.parseInt(id));
		} else {
			article = articleModel.findByTitleAndArticleClass(id, 8);
		}
		if (article == null) {
			return "redirect:/index.html";
		}
		model.addAttribute("article", article);
		return "article/detail";
	}

	@RequestMapping("/article.htm")
	public String pcarticle(Model model, Integer articleClassId) {
		model.addAttribute("articleClassId", articleClassId);
		ArticleClass articleClass = articleClassModel.get(articleClassId);
		model.addAttribute("articleClassTitle", articleClass.getName());
		return "article/pcarticle";
	}

	@RequestMapping("/home.htm")
	public String pchome() {
		return "article/pchome";
	}

	@RequestMapping("/list/{articleClassId}/{page}.htm")
	public String pclist(Model model, @PathVariable Integer articleClassId, @PathVariable Integer page) {
		Page<Article> articleList = articleModel.findArticleListByArticleClass(articleClassId, page, 5);
		if(articleList == null){
			model.addAttribute("articleList", null);
			model.addAttribute("count", 0);
		} else {
			model.addAttribute("articleList", articleList.getContent());
			model.addAttribute("count", articleList.getTotalPages());
		}
		return "article/pclist";
	}

	@RequestMapping("/detail/{id}.htm")
	public String pcdetail(Model model, @PathVariable Integer id) {
		Article article = articleModel.get(id);
		if(article == null){
			return "redirect:/index.htm";
		}
		model.addAttribute("article", article);
		model.addAttribute("articleClassName", article.getArticleClass().getName());
		return "article/pcdetail";
	}

}
